//
//  WSFavoritesRequest.m
//  CharonTVProject
//
//  Created by karong on 2023/8/2.
//

#import "WSFavoritesRequest.h"

@implementation WSFavoritesRequest

+ (void)loadcharonTV_FavoritesListWithPage:(NSInteger)page success:(charonTV_ArrayBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_BOOKSHELF;
    NSDictionary * parameters = @{@"type":@"1",
                                  @"page":@(page)
    };
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        NSMutableArray * array = [WSVideo mj_objectArrayWithKeyValuesArray:responseDict[@"items"]];
        success(array);
    } failure:failed];
}
+ (void)addcharonTV_FavoriteWithChapterId:(NSInteger)chapter_id tv_id:(NSInteger)tv_id success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSString * charonTV_url = API_CHARONTV_BOOKSHELF_ADD;
    NSDictionary * charonTV_parameters = @{@"chapter_id":@(chapter_id),
                                           @"tv_id":@(tv_id)
    };
    [self postWithURLString:charonTV_url parameters:charonTV_parameters success:^(NSDictionary * _Nonnull responseDict) {
        success();
    } failure:failed];
}

+ (void)delcharonTV_FavoriteWithTVIds:(NSArray *)tv_ids success:(charonTV_EmptyBlock)success failed:(Failure)failed {
    NSString * url = API_CHARONTV_BOOKSHELF_DEL;
    NSMutableString * ids = [NSMutableString new];
    for (WSVideo * video in tv_ids) {
        if (ids.length > 0) {
            [ids appendString:@","];
        }
        [ids appendString:WSSF(@"%ld", (long)video.tv_id)];
    }
    NSDictionary * parameters = @{@"tv_ids":ids,
    };
    [self postWithURLString:url parameters:parameters success:^(NSDictionary * _Nonnull responseDict) {
        success();
    } failure:failed];
}

#pragma mark - PagableRequestPotocol
- (void)refreshDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.haveMore = YES;
    self.page = 1;
    [WSFavoritesRequest loadcharonTV_FavoritesListWithPage:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}


- (void)loadMoreDataSuccess:(charonTV_ArrayBlock)success failed:(Failure)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    
    [WSFavoritesRequest loadcharonTV_FavoritesListWithPage:self.page success:^(NSArray *array) {
        [self loadSuccess:success withArray:array];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(charonTV_ArrayBlock)success withArray:(NSArray *)array {
    self.haveMore = array == 10;
    self.page++;
    self.loading = NO;
    success(array);
}
@end
